package com.yanqu.road.dao.impl.activity.turtle;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.activity.turtle.config.TurtleModel;
import com.yanqu.road.entity.activity.turtle.config.TurtleTemplate;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author : Cxp
 * @Time : 2024-09-06
 * @Description : 乌龟对对碰-Config Dao
 */

public class TurtleConfigDaoImpl extends TempDao {
    private static String getTemplateConfigSql = "SELECT * FROM t_s_activity_turtle_template WHERE activityId = ?";
    public Map<Integer, TurtleTemplate> getTemplateConfig(int activityId) {
        Map<Integer, TurtleTemplate>  dataMap = new HashMap<>();

        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getTemplateConfigSql);
            int i = 1;
            call.setInt(i, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                TurtleTemplate template = new TurtleTemplate(rs);
                dataMap.put(template.getId(), template);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }

        return dataMap;
    }

    private static String getModelConfigSql = "SELECT * FROM t_s_activity_turtle_model WHERE activityId = ?";
    public Map<Integer, TurtleModel> getModelConfig(int activityId) {
        Map<Integer, TurtleModel>  dataMap = new HashMap<>();

        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getModelConfigSql);
            int i = 1;
            call.setInt(i, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                TurtleModel model = new TurtleModel(rs);
                dataMap.put(model.getId(), model);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }

        return dataMap;
    }
}
